import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/index.vue'
import Layout from '@/views/layout/index.vue'
import store from '@/store/index'
const Dashboard = () => import('@/views/dashboard')
const Article = () => import('@/views/article')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      { path: 'dashboard', component: Dashboard },
      { path: 'article', component: Article }
    ]
  },
  { path: '/login', component: Login }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = store.state.user.token
    // token ? next() : next('/login')
    console.log(token)
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
